<?php
namespace App\HttpController;


class Article extends \App\Base\AuthController
{

    use \App\HttpController\Traits\Category;
    use \App\HttpController\Traits\Upload;

    public function __construct(){
        parent::__construct();
        $this->moduleid=11;
        $this->MD=$this->CORE_CONF->mod_single($this->moduleid);
        \copy_setting($this->moduleid);
        $this->setting=\load_setting($this->moduleid);
        $this->UNIQ=\uniq();
        $this->getView()->assign([
            'MODULEID'=>$this->moduleid,
            'MD'=>$this->MD,
            'CATEGORY_CACHE_URL'=>'/article/category_cache',
            'CATEGORY_MOD_URL'=>'/article/category_mod',
            'CATEGORY_DEL_URL'=>'/article/category_del',
            'UPLOAD_URL1'=>'/article/upload_img_action',
            'UPLOAD_URL2'=>'/article/upload_file_action',
            'UPLOAD_URL3'=>'/article/upload_img_tradition_action',
            'UPLOAD_URL4'=>'/article/upload_file_tradition_action',
            'UPLOAD_HUGE'=>'/article/upload_huge_action',
            'UNIQ'=>$this->UNIQ,
        ]);
        

    }

    //attachment hook
    protected function save_attachment($articleid,$uniq,$url){
        $model=new \App\Model\Article_attachmentModel();
        $model->articleid=$articleid;
        $model->uniq=$uniq;
        $model->url=$url;
        $model->ts=time();
        $model->save();
    }
    public function index()
    {
        $this->response()->write('article/index');
    }

    public function add(){
        $request=$this->request();
        $post=$request->getParsedBody();
        $model=new \App\Model\ArticleModel();
        if($post){
            $post=\dtrim($post);
            $post['addtime']=time();
            $post['edittime']=time();
            $post['userid']=$this->auth['userid'];
            $post['username']=$this->auth['username'];
            $post['editorid']=$this->auth['userid'];
            $post['editor']=$this->auth['username'];

            $vali=new \EasySwoole\Validate\Validate();
            $vali->addColumn('catid')->func(function($d){
                if($d['catid']==0) return false;
                return true;
            },'所属分类不能为空');
            $vali->addColumn('title')->lengthMin(1,'标题不能为空');
            $vali->addColumn('content')->lengthMin(1,'内容不能为空');
            
            $r=$vali->validate($post);
            if(!$r){
                $this->msg($r,$vali->getError()->__toString());
                return;
            }
            $model=\App\Model\ArticleModel::create($post);
            $insertid=$model->save();
            if($insertid){
                \App\Model\Article_attachmentModel::create()->where(['uniq'=>$post['UNIQ']])->update(['articleid'=>$insertid]);
            }
            $this->msg($insertid);
            return;
        }
        $category_model=new \App\Model\CategoryModel();
        $category=$category_model->category_select($this->moduleid,'catid',0,'请选择',' class="form-control" ');
        $flag=1;
        $this->fetch(__FUNCTION__,get_defined_vars());
    }

    public function lst(){
        $param = $this->request()->getRequestParam();
        extract($param);
        $model = new \App\Model\ArticleModel();
        $category_model=new \App\Model\CategoryModel();
        $where = [];
        $where['flag']=[[1,2],'in'];
        isset($itemid) or $itemid='';
        if($itemid){
            $where['itemid']=$itemid;
        }
        isset($catid) or $catid=0;
        if($catid){
            $where['catid']=$catid;
        }
        isset($title) or $title='';
        if($title){
            $where['title']=["%{$title}%",'like'];
        }
        isset($flag) or $flag=0;
        if($flag){
            $where['flag']=$flag;
        }
        $page = $param['page']??1;
        $pagesize = $this->setting['pagesize'];
        $data = $model->order('itemid','desc')->limit(($page-1)*$pagesize,$pagesize)->withTotalCount()->select($where);
        foreach($data as $k=>$v){
            $data[$k]['cat']=$category_model->get_category($v['catid']);
            $data[$k]['addtime']=\timeswitch($v['addtime']);
        }
        $total = $model->lastQueryResult()->getTotalCount();
        $pager=\pagination($this->request()->getServerParams()['request_uri'],$param,$page,$pagesize,$total);
        $category=$category_model->category_select($this->moduleid,'catid',$catid,'请选择',' class="form-control" ');
        $this->fetch(__FUNCTION__,get_defined_vars());
    }

    public function mod(){
        $request=$this->request();
        $get=$request->getQueryParams();
        extract($get);
        $itemid=intval($itemid);
		if(!$itemid){
            $this->msg(0,'lose itemid');
            return;
        }

        $raw=$post=$request->getParsedBody();
        $model=new \App\Model\ArticleModel();
        if($post){
            unset($post['submit'],$post['UNIQ'],$post['FORWARD']);
            $post=\dtrim($post);
            $post['edittime']=time();
            $post['editorid']=$this->auth['userid'];
            $post['editor']=$this->auth['username'];

            $vali=new \EasySwoole\Validate\Validate();
            $vali->addColumn('catid')->func(function($d){
                if($d['catid']==0) return false;
                return true;
            },'所属分类不能为空');
            $vali->addColumn('title')->lengthMin(1,'标题不能为空');
            $vali->addColumn('content')->lengthMin(1,'内容不能为空');
            
            $r=$vali->validate($post);
            if(!$r){
                $this->msg($r,$vali->getError()->__toString());
                return;
            }
            $model=\App\Model\ArticleModel::create();
            $d=$model->get($itemid);
            $r=$d->update($post);
            if($r){
                \App\Model\Article_attachmentModel::create()->where(['uniq'=>$raw['UNIQ']])->update(['articleid'=>$itemid]);
                $model->treat_attachment($itemid);
            }
            $this->msg($r,'',$raw['FORWARD']);
            return;
        }
        $data=$model->where(['itemid'=>$itemid])->get();
		if(!$data){
            $this->msg(0,'数据不存在');
            return;
        }
        $category_model=new \App\Model\CategoryModel();
        $category=$category_model->category_select($this->moduleid,'catid',$data['catid'],'请选择',' class="form-control" ');
        $this->fetch(__FUNCTION__,get_defined_vars());
    }

    public function del(){
        $request=$this->request();
        $get=$request->getQueryParams();
        extract($get);

        $itemid=intval($itemid);
		if(!$itemid){
            $this->msg(0,'lose itemid');
            return;
        }
        $model=\App\Model\ArticleModel::create();
        $d=$model->where(['itemid'=>$itemid])->get();
        if(!$d){
            $this->msg(0,'数据不存在');
            return;
        }
        //删除thumb
        //删除附件
        if(false){
            $attachments=\App\Model\Article_attachmentModel::create()->where('articleid',$itemid)->select();
            foreach($attachments as $k=>$v){
                $file=EASYSWOOLE_ROOT.'/public'.$v['url'];
                if(!file_exists($file)) continue;
                unlink($file);
            }
            \App\Model\Article_attachmentModel::create()->where('articleid',$itemid)->destroy();
        }
        $r=$d->update(['flag'=>3]);//$d->destroy();
		$this->msg($r===false?false:true);
    }

    public function preview(){
        $request=$this->request();
        $get=$request->getQueryParams();
        extract($get);
        $itemid=intval($itemid);
		if(!$itemid){
            $this->msg(0,'lose itemid');
            return;
        }
        $model=\App\Model\ArticleModel::create();
        $data=$model->where(['itemid'=>$itemid])->get();
		if(!$data){
            $this->msg(0,'数据不存在');
            return;
        }
        $category_model=new \App\Model\CategoryModel();
        $cat=$category_model->get_category($data['catid']);
        $this->fetch(__FUNCTION__,get_defined_vars());
    }

    public function outside(){
        $param = $this->request()->getRequestParam();
        extract($param);

        if(!$callback){
            $this->msg(0,'lose callback');
            return;
        }

        $model = new \App\Model\ArticleModel();
        $where = [];
        $where['flag']=[[1,2],'in'];
        isset($itemid) or $itemid='';
        if($itemid){
            $where['itemid']=$itemid;
        }
        isset($catid) or $catid=0;
        if($catid){
            $where['catid']=$catid;
        }
        isset($title) or $title='';
        if($title){
            $where['title']=["%{$title}%",'like'];
        }
        isset($flag) or $flag=0;
        if($flag){
            $where['flag']=$flag;
        }
        $page = $param['page']??1;
        $pagesize = $this->setting['pagesize'];
        $data = $model->order('itemid','desc')->limit(($page-1)*$pagesize,$pagesize)->withTotalCount()->select($where);
        $total = $model->lastQueryResult()->getTotalCount();
        $pager=\pagination($this->request()->getServerParams()['request_uri'],$param,$page,$pagesize,$total);
        $category_model=new \App\Model\CategoryModel();
        $category=$category_model->category_select($this->moduleid,'catid',$catid,'请选择',' class="form-control" ');
        $this->fetch(__FUNCTION__,get_defined_vars());
    }

    public function setting(){
        $request=$this->request();
        $post = $request->getParsedBody();
        if($post){
            $r=\save_setting($this->moduleid,$post);
			$this->msg($r);
        }
        $setting=\load_setting($this->moduleid);
        $this->fetch(__FUNCTION__,get_defined_vars());
    }
    public function clear_attachments(){
        $attachments=\App\Model\Article_attachmentModel::create()->where(" ts < ? ",[time()-86400])->where('articleid',0)->select();
        foreach($attachments as $k=>$v){
            $file=EASYSWOOLE_ROOT.'/public'.$v['url'];
            if(!file_exists($file)) continue;
            unlink($file);
        }
        \App\Model\Article_attachmentModel::create()->where(" ts < ? ",[time()-86400])->where('articleid',0)->destroy();
        $this->msg(1);
    }
    public function test(){
        \p($this->get('itemid'));
        
        $this->fetch(__FUNCTION__,get_defined_vars());
    }

}